import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from "js-cookie";
Vue.use(VueRouter)

const routes = [{
  path: '/',
  name: 'index',
  component: () => import('../views/index.vue'),
  redirect: '/dashboard',
  children: [
    {
      path: '/dashboard',
      name: 'dashboard',
      component: () => import('../views/dashboard/index.vue'),
    },
    {
      path: '/echarts',
      name: 'echarts',
      component: () => import('@/layout'),
      children: [
        {
          path: '/echarts/bar',
          name: 'bar',
          component: () => import('@/views/echarts/echartsBar.vue'),
        },
        {
          path: '/echarts/lines',
          name: 'lines',
          component: () => import('@/views/echarts/echartsLines.vue'),
        },
        {
          path: '/echarts/pie',
          name: 'pie',
          component: () => import('@/views/echarts/echartsPie.vue'),
        },
        {
          path: '/echarts/mix',
          name: 'dashboard',
          component: () => import('@/views/echarts/echartsMix.vue'),
        },
        {
          path: '/echarts/gaodeMap',
          name: 'gaodeMap',
          component: () => import('@/views/echarts/gaodeMap.vue'),
        },
        {
          path: '/echarts/echartsMap',
          name: 'echartsMap',
          component: () => import('@/views/echarts/echartsMap.vue'),
        },
      ]
    },
    {
      path: '/message',
      name: 'message',
      component: () => import('../views/message/index.vue'),
    },
    {
      path: '/testroles',
      name: 'testroles',
      component: () => import('../views/testroles/index.vue'),
    },
    {
      path: '/testpession',
      name: 'testpession',
      component: () => import('../views/testpession/index.vue'),
    },
    {
      path: '/testOther',
      name: 'testOther',
      component: () => import('../views/other/index.vue'),
    },
    {
      path: '/tables',
      name: 'tables',
      component: () => import('../views/tables/index.vue'),
    },

  ]
},
{
  path: '/login',
  name: 'login',
  component: () => import('../views/login/index.vue'),
}
]
const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    return savedPosition || { x: 0, y: 0 }; // 设置为滚动到页面顶部
  }
})
// 路由白名单
const whiteList = ['/login']

// 路由前置守卫
router.beforeEach((to, from, next) => {
  // 一般存在cookie里面并且进行加密，这里简单处理了
  const token = Cookies.get('token') ? Cookies.get('token') : ''
  // 模拟登录后会存token
  if (token) {
    // 阻止页面回退到登录页
    if (to.path == '/login') {
      next('/dashboard')
    } else {
      next()
    }

  } else {
    if (to.path == '/login') {
      next()
    } else {
      next('/login')
    }
  }

})
// 路由后置守卫
router.afterEach(() => {
})
export default router